package logic

import (
	"context"
	"ewaimai/waimairpc/cache"
	"ewaimai/waimairpc/models"

	"ewaimai/waimairpc/internal/svc"
	"ewaimai/waimairpc/waimairpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *waimairpc.RegisterRequest) (*waimairpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line

	var err error
	var u models.User
	u.ID = uint(in.UserId)
	if u.ID == 0 {
		logx.Error("用户不存在")
	}
	if err = u.GetUserById(int(in.UserId)); err != nil {
		logx.Errorf("该用户已存在,不能重复注册")
	}
	u.Mobile = in.Mobile
	get := cache.Rdb.Get(l.ctx, "sendSms"+in.Mobile)
	if get.Val() != in.SendCode {
		logx.Errorf("短信验证码获取失败")
	}
	if err = u.Create(); err != nil {
		logx.Errorf("注册失败")
	}
	return &waimairpc.RegisterResponse{
		Id: int64(u.ID),
	}, nil
}
